package ${package.Controller};



<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

import com.easonji.core.annotion.Permission;
import com.easonji.core.log.LogObjectHolder;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.base.response.Response;
import com.easonji.common.base.response.enums.ResponseEnum;
import com.easonji.common.util.StringUtils;

import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.BindingResult;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Controller
@RequestMapping("/${package.ModuleName}/${table.entityPath}")
public class ${table.controllerName} extends ${superControllerClass} {


    /**
    * ${table.comment!}页面前缀
    */
    private final static String PREFIX = "${package.ModuleName}/${table.entityPath}/";

    @Resource
    private ${table.serviceName} ${table.entityPath}Service;

    /**
    * 跳转到${table.comment!}首页
    */
    @Permission
    @GetMapping("")
    public String index() {
        return PREFIX + "${table.entityPath}";
    }

    /**
    * 查询${table.comment!}列表
    */
    @ResponseBody
    @PostMapping("/list")
    public Object queryList(BaseQuery baseQuery) {
        Object result = ${table.entityPath}Service.queryList(baseQuery);
        return result;
    }

    /**
    * 跳转到添加${table.comment!}页面
    */
    @Permission
    @GetMapping("/to_add")
    public String toAdd(Model model) {
        return PREFIX + "${table.entityPath}_edit";
    }

    /**
    * 跳转到修改${table.comment!}页面
    */
    @Permission
    @GetMapping("/to_update/{${table.fields[0].propertyName}}")
    public String toUpdate(@PathVariable Integer ${table.fields[0].propertyName}, Model model) {
        Object result = ${table.entityPath}Service.queryRowById(${table.fields[0].propertyName});
        LogObjectHolder.me().set(result);
        model.addAttribute("${table.entityPath}", result);
        return PREFIX + "${table.entityPath}_edit";
    }

    /**
    * 新增${table.comment!}
    */
    @ResponseBody
    @Permission
    @PostMapping("/save")
    public Response add(@Valid ${entity} ${table.entityPath}, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(bindingResult);
        }
        int result = ${table.entityPath}Service.save(${table.entityPath});
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.SAVE_SUCCESS);
    }

    /**
    * 修改${table.comment!}
    */
    @ResponseBody
    @Permission
    @PostMapping("/update")
    public Response update(@Valid ${entity} ${table.entityPath}, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(bindingResult);
        }
        int result = ${table.entityPath}Service.update(${table.entityPath});
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
    * 删除${table.comment!}
    */
    @ResponseBody
    @Permission
    @PostMapping("/remove")
    public Response remove(@RequestParam Integer ${table.fields[0].propertyName}) {
        if (StringUtils.isEmpty(${table.fields[0].propertyName})) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        int result = ${table.entityPath}Service.delete(${table.fields[0].propertyName});
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.REMOVE_SUCCESS);
    }

}
